//
//  UIView+ZZUtil.m
//  OCFrame
//
//  Created by hfzhangzhang on 2016/12/19.
//  Copyright © 2016年 hfzhangzhang. All rights reserved.
//

#import "UIView+ZZUtil.h"
#import "objc/runtime.h"
#import "DynamicTools.h"

@implementation UIView (ZZUtil)

@end


@implementation UIView (geometry)

- (void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)x
{
    return self.frame.origin.x;
}

- (CGFloat)y
{
    return self.frame.origin.y;
}

- (void)setCenterX:(CGFloat)centerX
{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)centerX
{
    return self.center.x;
}

- (void)setCenterY:(CGFloat)centerY
{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (CGFloat)centerY
{
    return self.center.y;
}

- (void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)height
{
    return self.frame.size.height;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (void)setSize:(CGSize)size
{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGSize)size
{
    return self.frame.size;
}

- (void)setOrigin:(CGPoint)origin
{
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGPoint)origin
{
    return self.frame.origin;
}

- (void)setMinX:(CGFloat)minX{
    CGRect frame = self.frame;
    frame.origin.x = minX;
    self.frame = frame;
}

- (CGFloat)minX{
    return CGRectGetMinX(self.frame);
}

- (void)setMinY:(CGFloat)minY{
    CGRect frame = self.frame;
    frame.origin.y = minY;
    self.frame = frame;
}

- (CGFloat)minY{
    return CGRectGetMinY(self.frame);
}

- (void)setMaxX:(CGFloat)maxX{
    self.maxX = self.x +self.width;
}

- (CGFloat)maxX{
    return CGRectGetMaxX(self.frame);
}

- (void)setMaxY:(CGFloat)maxY{
    self.maxY = self.y + self.height;
}

- (CGFloat)maxY{
    return CGRectGetMaxY(self.frame);
}

-(void)zz_setMaskRadius:(float)radius{
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = radius;
}

- (void)zz_cornerRadius:(CGFloat)radius borderColor:(UIColor*)borderColor borderWidth:(CGFloat)borderWidth {
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius  = radius;
    self.layer.borderColor = borderColor.CGColor;
    self.layer.borderWidth = borderWidth;
}

- (void)zz_makeRoundedCorner:(UIRectCorner)byRoundingCorners cornerRadii:(CGSize)cornerRadii {
    UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:byRoundingCorners cornerRadii:cornerRadii];
    CAShapeLayer* shape = [CAShapeLayer layer];
    shape.path = path.CGPath;
    self.layer.mask = shape;
}

// 对UIView的四个角进行选择性的圆角处理
- (void)makeRoundedCorner:(UIRectCorner)byRoundingCorners cornerRadii:(CGSize)cornerRadii{
    UIBezierPath* path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:byRoundingCorners cornerRadii:cornerRadii];
    CAShapeLayer* shape = [CAShapeLayer layer];
    shape.path = path.CGPath;
    self.layer.mask = shape;
}

@end

@interface  UIView ()
@property (nonatomic, copy) void(^tapAction)(id);
@end

@implementation UIView (gesture)

- (void)tap{
    if (self.tapAction) {
        self.tapAction(self);
    }
}
- (void)zz_addTapBlock:(void(^)(id obj))tapAction{
    self.tapAction = tapAction;
    if (![self gestureRecognizers]) {
        self.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
        [self addGestureRecognizer:tap];
    }
}

@end



@implementation UIView (screenshot)

- (UIImage *)screenshot {
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
    
    // IOS7及其后续版本
    if ([self respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:
                                    [self methodSignatureForSelector:
                                     @selector(drawViewHierarchyInRect:afterScreenUpdates:)]];
        [invocation setTarget:self];
        [invocation setSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)];
        CGRect arg2 = self.bounds;
        BOOL arg3 = YES;
        [invocation setArgument:&arg2 atIndex:2];
        [invocation setArgument:&arg3 atIndex:3];
        [invocation invoke];
    } else { // IOS7之前的版本
        [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    }
    
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return screenshot;
}

@end

@interface UIView ()

@property (nonatomic, assign, getter=isEmpty) BOOL empty;

@end

static char TAG_EMPTY;
static char TAG_EMPTY_VIEW;

@implementation UIView (empty)

- (void)setEmptyView:(UIView *)emptyView {
    emptyView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    if (self.isEmpty) {
        UIView *oldEmptyView = objc_getAssociatedObject(self, &TAG_EMPTY_VIEW);
        [oldEmptyView removeFromSuperview];
        emptyView.frame = self.bounds;
        [self addSubview:emptyView];
    }
    objc_setAssociatedObject(self, &TAG_EMPTY_VIEW, emptyView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIView *)emptyView {
    UIView *emptyView = objc_getAssociatedObject(self, &TAG_EMPTY_VIEW);
    return emptyView;
}


- (BOOL)isEmpty {
    return [objc_getAssociatedObject(self, &TAG_EMPTY) boolValue];
}

- (void)setEmpty:(BOOL)empty {
    [self setEmpty:empty animated:NO];
}

- (void)setEmpty:(BOOL)empty animated:(BOOL)animated {
    if (empty == self.isEmpty) return;
    
    objc_setAssociatedObject(self, &TAG_EMPTY, @(empty), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    if (empty) {
        self.emptyView.frame = self.bounds;
        if (animated) {
            self.emptyView.alpha = 0;
            [self addSubview:self.emptyView];
            [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
                self.emptyView.alpha = 1;
            } completion:nil];
        } else {
            [self addSubview:self.emptyView];
        }
    } else {
        
        if (animated) {
            [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
                self.emptyView.alpha = 0;
            } completion:^(BOOL finished) {
                if (self.isEmpty) [self.emptyView removeFromSuperview];
            }];
        } else {
            [self.emptyView removeFromSuperview];
        }
        
    }
}

@end


static char TAG_VIEW_TOUCH_AREA_EXPAND_INSETS;
@interface  UIView ()
@property (nonatomic, assign) UIEdgeInsets expandInsets;
@end
@implementation UIView (IUTouchAreaExpand)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class aClass = [self class];
        Swizzle_(aClass, NSSelectorFromString(@"pointInside:withEvent:"), @selector(touchAreaExpand_pointInside:withEvent:));
    });
}

- (void)zz_setEnlargeEdge:(CGFloat)edge {
    self.expandInsets = UIEdgeInsetsMake(edge, edge, edge, edge);
}
- (void)zz_setEnlargeEdgeWithTop:(CGFloat) top right:(CGFloat) right bottom:(CGFloat) bottom left:(CGFloat) left{
     self.expandInsets = UIEdgeInsetsMake(top, right, bottom, left);
}

- (void)setExpandInsets:(UIEdgeInsets)expandInsets {
    objc_setAssociatedObject(self, &TAG_VIEW_TOUCH_AREA_EXPAND_INSETS, [NSValue valueWithUIEdgeInsets:expandInsets], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIEdgeInsets)expandInsets {
    return [objc_getAssociatedObject(self, &TAG_VIEW_TOUCH_AREA_EXPAND_INSETS) UIEdgeInsetsValue];
}

- (BOOL)touchAreaExpand_pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    BOOL result = [self touchAreaExpand_pointInside:point withEvent:event];
    UIEdgeInsets expandInsets = self.expandInsets;
    if (result || UIEdgeInsetsEqualToEdgeInsets(expandInsets, UIEdgeInsetsZero)) {
        return result;
    } else {
        CGRect rect = self.bounds;
        rect.origin.x -= expandInsets.left;
        rect.origin.y -= expandInsets.top;
        rect.size.width += expandInsets.left + expandInsets.right;
        rect.size.height += expandInsets.top + expandInsets.bottom;
        return CGRectContainsPoint(rect, point);
    }
}

@end




